//
//  UIView+Helper.m
//  LZLife
//
//  Created by 214 on 2021/3/16.
//

#import "UIView+Helper.h"

@implementation UIView (Helper)

- (void)setCustomX:(CGFloat)customX {
    CGRect frame = self.frame;
    frame.origin.x = customX;
    self.frame = frame;
}

- (void)setCustomY:(CGFloat)customY {
    CGRect frame = self.frame;
    frame.origin.y = customY;
    self.frame = frame;
}

- (CGFloat)customX {
    return self.frame.origin.x;
}

- (CGFloat)customY {
    return self.frame.origin.y;
}

- (void)setCustomCenterX:(CGFloat)customCenterX {
    CGPoint center = self.center;
    center.x = customCenterX;
    self.center = center;
}

- (CGFloat)customCenterX {
    return self.center.x;
}

- (void)setCustomCenterY:(CGFloat)customCenterY {
    CGPoint center = self.center;
    center.y = customCenterY;
    self.center = center;
}

- (CGFloat)customCenterY {
    return self.center.y;
}

- (void)setCustomWidth:(CGFloat)customWidth {
    CGRect frame = self.frame;
    frame.size.width = customWidth;
    self.frame = frame;
}

- (void)setCustomHeight:(CGFloat)customHeight {
    CGRect frame = self.frame;
    frame.size.height = customHeight;
    self.frame = frame;
}

- (CGFloat)customHeight {
    return self.frame.size.height;
}

- (CGFloat)customWidth {
    return self.frame.size.width;
}

- (void)setCustomSize:(CGSize)customSize {
    CGRect frame = self.frame;
    frame.size = customSize;
    self.frame = frame;
}

- (CGSize)customSize {
    return self.frame.size;
}

- (void)setCustomOrigin:(CGPoint)customOrigin {
    CGRect frame = self.frame;
    frame.origin = customOrigin;
    self.frame = frame;
}

- (CGPoint)customOrigin {
    return self.frame.origin;
}

- (CGFloat)customTop {
    return self.frame.origin.y;
}

- (void)setCustomTop:(CGFloat)customTop {
    CGRect frame = self.frame;
    frame.origin.y = customTop;
    self.frame = frame;
}

- (CGFloat)customLeft {
    return self.frame.origin.x;
}

- (void)setCustomLeft:(CGFloat)customLeft {
    CGRect frame = self.frame;
    frame.origin.x = customLeft;
    self.frame = frame;
}

- (CGFloat)customBottom {
    return self.frame.size.height + self.frame.origin.y;
}

- (void)setCustomBottom:(CGFloat)customBottom {
    CGRect frame = self.frame;
    frame.origin.y = customBottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)customRight {
    return self.frame.size.width + self.frame.origin.x;
}

- (void)setCustomRight:(CGFloat)customRight {
    CGRect frame = self.frame;
    frame.origin.x = customRight - frame.size.width;
    self.frame = frame;
}

- (void)rounded:(CGFloat)cornerRadius {
    [self rounded:cornerRadius width:0 color:nil];
}

- (void)border:(CGFloat)borderWidth color:(UIColor *)borderColor {
    [self rounded:0 width:borderWidth color:borderColor];
}

- (void)rounded:(CGFloat)cornerRadius width:(CGFloat)borderWidth color:(UIColor *)borderColor {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = cornerRadius;
    self.layer.borderWidth = borderWidth;
    self.layer.borderColor = [borderColor CGColor];
}


- (void)round:(CGFloat)cornerRadius RectCorners:(UIRectCorner)rectCorner {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)round:(CGFloat)cornerRadius RectCorners:(UIRectCorner)rectCorner  width:(CGFloat)borderWidth color:(UIColor *)borderColor {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;

    maskLayer.lineWidth = borderWidth;
    maskLayer.fillColor = [UIColor clearColor].CGColor;
    maskLayer.strokeColor = borderColor.CGColor;
    [self.layer addSublayer:maskLayer];
    
}

- (void)shadow:(UIColor *)shadowColor opacity:(CGFloat)opacity radius:(CGFloat)radius offset:(CGSize)offset {
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOpacity = opacity;
    self.layer.shadowRadius = radius;
    self.layer.shadowOffset = offset;
}

- (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
    theView.layer.masksToBounds = NO;
    // 阴影颜色
    theView.layer.shadowColor = theColor.CGColor;
    // 阴影偏移，默认(0, -3)
    theView.layer.shadowOffset = CGSizeMake(0,0);
    // 阴影透明度，默认0
    theView.layer.shadowOpacity = 0.2;
    // 阴影半径，默认3
    theView.layer.shadowRadius = 7.5;
}

- (UIViewController *)viewController {
    id nextResponder = [self nextResponder];
    while (nextResponder != nil) {
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vc = (UIViewController *)nextResponder;
            return vc;
        }
        nextResponder = [nextResponder nextResponder];
    }
    return nil;
}

+ (CGFloat)getLabelHeightByWidth:(CGFloat)width Title:(NSString *)title font:(UIFont *)font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}


/**
 设置view指定位置的边框
 
 @param color          边框颜色
 @param borderWidth    边框宽度
 @param borderType     边框类型 例子: UIBorderSideTypeTop|UIBorderSideTypeBottom
 */
- (void)borderViewForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
      
    if (borderType == UIBorderSideTypeAll) {
        self.layer.borderWidth = borderWidth;
        self.layer.borderColor = color.CGColor;
    }
      
    /// 线的路径
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
      
    /// 左侧
    if (borderType & UIBorderSideTypeLeft) {
        /// 左侧线路径
        [bezierPath moveToPoint:CGPointMake(0.0f, self.frame.size.height)];
        [bezierPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
    }
      
    /// 右侧
    if (borderType & UIBorderSideTypeRight) {
        /// 右侧线路径
        [bezierPath moveToPoint:CGPointMake(self.frame.size.width, 0.0f)];
        [bezierPath addLineToPoint:CGPointMake( self.frame.size.width, self.frame.size.height)];
    }
      
    /// top
    if (borderType & UIBorderSideTypeTop) {
        /// top线路径
        [bezierPath moveToPoint:CGPointMake(0.0f, 0.0f)];
        [bezierPath addLineToPoint:CGPointMake(self.frame.size.width, 0.0f)];
    }
      
    /// bottom
    if (borderType & UIBorderSideTypeBottom) {
        /// bottom线路径
        [bezierPath moveToPoint:CGPointMake(0.0f, self.frame.size.height)];
        [bezierPath addLineToPoint:CGPointMake( self.frame.size.width, self.frame.size.height)];
    }
    
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路径
    shapeLayer.path = bezierPath.CGPath;
    /// 线宽度
    shapeLayer.lineWidth = borderWidth;
      
    [self.layer addSublayer:shapeLayer];
      
}

@end
